<?php

namespace App\Http\Controllers\Admin;


use App\Common\CommonHelpers;
use App\Http\Controllers\Controller;
use App\Models\RsConfig;
use Illuminate\Http\Request;

class ConfigController extends Controller{

    public function index(Request $request){
        return view('admin.config.index');
    }

    public function apiList(Request $request){
        //查询消息数据
        $data=RsConfig::lists($request->input('page', 1), $request->input('limit', 20), $request->input());
        return response($data ? array_merge(['code' => 0, 'msg' => '查询成功'],  $data) : ['code' => 906, 'msg' => '当前没有更多数据']);
    }

    //创建数据
    public function create(Request $request){

        if($request->wantsJson()){
            $data=[
                "attr_name"=>$request->input('attr_name'),
                'attr_value'=>$request->input('attr_value'),
                'create_time'=>date('Y-m-d H:i:s')
            ];
            if(!$request->input('id')){
                $data=RsConfig::create($data);
            }else{
               // $data['id']=$request->input('id');
                $data=RsConfig::query()->where(['id'=>$request->input('id')])->update($data);
            }

            if($data){
                return CommonHelpers::responseFormat(0,'','添加成功');

            }else{
                return CommonHelpers::responseFormat(0,'','添加失败');

            }
        }
        return view('admin.config.create',['model'=>[]]);
    }

    //删除数据
    public function delete(Request $request){
        $id=$request->input('id');
        $data=RsConfig::query()->where(['id'=>$id])->delete();
        if($data){
            return CommonHelpers::responseFormat(0,'','删除成功');

        }else{
            return CommonHelpers::responseFormat(0,'','删除失败');

        }
    }

    //修改数据
    public function edit(Request $request,$id){
        $model=RsConfig::query()->where(['id'=>$id])->first();
        return view('admin.config.create',['model'=>$model]);

    }

}
